﻿using AviagePortal.Entity;
using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AviagePortal
{
    class JsonGenerator
    {
       static public void GenerateJsonForGlobalSettingsItem(SPWeb web, string[] paths)
        {
            // GlobalSettings [Chinese, English,PC,Mobile]
            List<string> keys = new List<string>();
            SPList list = web.Lists["GlobalSettings"];
            List<string> pathsList = paths.ToList<string>();
            foreach (SPListItem item in list.Items)
            {
                string key = "/" + item["Language"] +  "/" + item["DeviceType"] + "/";
                if (!keys.Contains(key) && pathsList.Contains(key))
                {
                    GlobalSettingsItem globalSettingsItem = new GlobalSettingsItem();
                    globalSettingsItem.CNText = "" + item["CNText"];
                    globalSettingsItem.CNUrl = "" + item["CNUrl"];
                    globalSettingsItem.ContactUSText = "" + item["ContactUSText"];
                    globalSettingsItem.ContactUSUrl = "" + item["ContactUSUrl"];
                    globalSettingsItem.CopyRight = "" + item["CopyRight"];
                    globalSettingsItem.ENText = "" + item["ENText"];
                    globalSettingsItem.ENUrl = "" + item["ENUrl"];
                    globalSettingsItem.HomeText = "" + item["HomeText"];
                    globalSettingsItem.HomeUrl = "" + item["HomeUrl"];
                    globalSettingsItem.INIcon = "" + item["INIcon"];
                    globalSettingsItem.INUrl = "" + item["INUrl"];
                    globalSettingsItem.Logo = "" + item["Logo"];
                    globalSettingsItem.SiteMapText = "" + item["SiteMapText"];
                    globalSettingsItem.SiteMapUrl = "" + item["SiteMapUrl"];
                    globalSettingsItem.UseTermText = "" + item["UseTermText"];
                    globalSettingsItem.UseTermUrl = "" + item["UseTermUrl"];
                    globalSettingsItem.PageTitle = "" + item["PageTitle"];
                    globalSettingsItem.BottomLinks = new List<BottomLink>();
                    globalSettingsItem.BottomMenus = new List<BottomMenu>();
                    for (int i = 1; i <= 5; i++)
                    {
                        string textMKey= "BottomMenu"+i+"Text";
                        string urlMkey = "BottomMenu" + i + "Url";
                        if (!string.IsNullOrWhiteSpace("" + item[textMKey]) && !string.IsNullOrWhiteSpace("" + item[urlMkey]))
                        {
                            BottomMenu menu = new BottomMenu();
                            menu.Text = "" + item[textMKey];
                            menu.Url = "" + item[urlMkey];
                            globalSettingsItem.BottomMenus.Add(menu);
                        }

                        string textLkey = "BottomLink"+i+ "Text";
                        string urlLkey = "BottomLink"+i+"Url";
                        if (!string.IsNullOrWhiteSpace("" + item[textLkey]) && !string.IsNullOrWhiteSpace("" + item[urlLkey]))
                        {
                            BottomLink link = new BottomLink();
                            link.Text = "" + item[textLkey];
                            link.Url = "" + item[urlLkey];
                            globalSettingsItem.BottomLinks.Add(link);
                        }
                    }

                    
                    // Json 
                    System.Web.Script.Serialization.JavaScriptSerializer jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                    var josnResult = "window.globalSettings =" + jsonSerializer.Serialize(globalSettingsItem);                  
                    var path = web.Url + "/SiteData/" + item["Language"] + "/" + item["DeviceType"] + "/globalSettings.json.js";
                    WriteFileToSharePointSite(web, path, josnResult);
                }
              
            }
        }

        private static void WriteFileToSharePointSite(SPWeb web, string path, string content)
        {
            Byte[] bytes = Encoding.UTF8.GetBytes(content);
            web.Files.Add(path, bytes, true);
        }

        public static void GenerateJsonForHomePageItem(SPWeb web, string[] paths)
        {
            // HomePage
            // Chinese, English,PC,Mobile
            SPList list = web.Lists["HomePage"];
            Dictionary<string, List<HomePageItem>> dic = new Dictionary<string, List<HomePageItem>>();
            //dic.Add("/Chinese/PC/", new List<HomePageItem>());
            //dic.Add("/Chinese/Mobile/", new List<HomePageItem>());
            //dic.Add("/English/PC/", new List<HomePageItem>());
            //dic.Add("/English/Mobile/", new List<HomePageItem>());
            foreach (var path in paths)
            {
                dic.Add(path, new List<HomePageItem>());
            }

            foreach (SPListItem item in list.Items)
            {
                string key = "/" + item["Language"] + "/" + item["DeviceType"]+ "/";
                if (dic.ContainsKey(key) && (!string.IsNullOrWhiteSpace("" + item["Title"]) && !string.IsNullOrWhiteSpace("" + item["PictureUrl"])))
                {
                    // dic[key]
                    HomePageItem homePageItem = new HomePageItem();
                    homePageItem.ID = item.ID.ToString();
                    homePageItem.Title = "" +item["Title"];
                    homePageItem.PictureUrl = "" + item["PictureUrl"];
                    homePageItem.LinkUrl = "" + item["LinkUrl"];
                    homePageItem.Order = item["Order0"] == null ? 99999 : int.Parse(item["Order0"].ToString());
                    dic[key].Add(homePageItem);
                }
            }

            foreach (var key in dic.Keys)
            {
                if (dic[key].Count > 0)
                {
                    System.Web.Script.Serialization.JavaScriptSerializer jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                    var josnResult = "window.HomePage =" + jsonSerializer.Serialize(dic[key]);
                    var path = web.Url + "/SiteData" + key + "HomePage.json.js";
                    WriteFileToSharePointSite(web, path, josnResult);
                }
            }
        }

        public static void GenerateJsonForProduct(SPWeb web, string[] paths)
        {
            SPList list = web.Lists["Product"];
            Dictionary<string, List<ProductItem>> dic = new Dictionary<string, List<ProductItem>>();
            //dic.Add("/Chinese/PC/", new List<ProductItem>());
            //dic.Add("/Chinese/Mobile/", new List<ProductItem>());
            //dic.Add("/English/PC/", new List<ProductItem>());
            //dic.Add("/English/Mobile/", new List<ProductItem>());
            foreach(var path in paths)
            {
                dic.Add(path, new List<ProductItem>());
            }

            foreach (SPListItem item in list.Items)
            {
                string key = "/" + item["Language"] + "/" + item["DeviceType"] + "/";
                if (dic.ContainsKey(key) && (!string.IsNullOrWhiteSpace("" + item["Title"])))
                {
                    // dic[key]
                    ProductItem productItem = new ProductItem();
                    productItem.ID = item.ID.ToString();
                    productItem.Title = "" + item["Title"];
                    productItem.Order = item["Order0"] == null ? 99999 : int.Parse(item["Order0"].ToString());
                    productItem.ArticleTitle = ""+ item["ArticleTitle"];
                    productItem.Introduction = "" + item["Introduction"];
                    productItem.Feature = "" + item["Feature"];
                    productItem.PageTitle = "" + item["PageTitle"];
                    productItem.KeyFeatureLabel = "" + item["KeyFeatureLabel"];
                    productItem.IntroduceLabel = "" + item["IntroduceLabel"];
                    productItem.Pictures = new List<PictureItem>();
                    for (int i = 1; i <= 5; i++)
                    {
                        string largePicture = "LargePicture" + i;
                        string smallPicture = "SmallPicture" + i;
                        if (!string.IsNullOrWhiteSpace("" + item[largePicture]) && !string.IsNullOrWhiteSpace("" + item[smallPicture]) || i==1)
                        {
                            PictureItem pictureItem = new PictureItem();
                            pictureItem.LargePicture = "" + item[largePicture];
                            pictureItem.SmallPicture = "" + item[smallPicture];
                            productItem.Pictures.Add(pictureItem);
                        }

                    }
                    dic[key].Add(productItem);
                }

                dic[key] = dic[key].OrderBy(t => t.Order).ToList();
            }

            foreach (var key in dic.Keys)
            {
                if (dic[key].Count > 0)
                {
                    System.Web.Script.Serialization.JavaScriptSerializer jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                    var josnResult = "window.Products =" + jsonSerializer.Serialize(dic[key]);
                    var path = web.Url + "/SiteData" + key + "Product.json.js";
                    WriteFileToSharePointSite(web, path, josnResult);
                }
            }
        }

        // CompanyIntroduce
        public static void GenerateJsonForCompanyIntroduce(SPWeb web, string[] paths)
        {
            SPList list = web.Lists["CompanyIntroduce"];
            Dictionary<string, List<CompanyIntroduceItem>> dic = new Dictionary<string, List<CompanyIntroduceItem>>();

            foreach (var path in paths)
            {
                dic.Add(path, new List<CompanyIntroduceItem>());
            }

            foreach (SPListItem item in list.Items)
            {
                string key = "/" + item["Language"] + "/" + item["DeviceType"] + "/";
                if (dic.ContainsKey(key) && (!string.IsNullOrWhiteSpace("" + item["Title"])))
                {
                    CompanyIntroduceItem companyIntroduceItem = new CompanyIntroduceItem();
                    companyIntroduceItem.Title = "" +item["Title"];
                    companyIntroduceItem.PageTitle = "" + item["PageTitle"];
                    companyIntroduceItem.Description1 = "" + item["Description1"];
                    companyIntroduceItem.Description2 = "" + item["Description2"];
                    companyIntroduceItem.Picture = "" + item["Picture"];
                    dic[key].Add(companyIntroduceItem);
                }
            }


            foreach (var key in dic.Keys)
            {
                if (dic[key].Count > 0)
                {
                    System.Web.Script.Serialization.JavaScriptSerializer jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                    var josnResult = "window.CompanyIntroduce =" + jsonSerializer.Serialize(dic[key]);
                    var path = web.Url + "/SiteData" + key + "CompanyIntroduce.json.js";
                    WriteFileToSharePointSite(web, path, josnResult);
                }
            }
        }

        // CompanyHistory
        public static CompanyHistoryItem GetCompanyHistoryItem(SPWeb web, string language, string deveiceType)
        {
              SPList list = web.Lists["LeaderShipMain"];
            string path = "/"+language + "/" + deveiceType + "/";
            CompanyHistoryItem companyHistoryItem = null;
            foreach (SPListItem item in list.Items)
            {
                string key = "/" + item["Language"] + "/" + item["DeviceType"] + "/";
                if (key == path)
                {
                    companyHistoryItem = new CompanyHistoryItem();
                    companyHistoryItem.ID = item.ID.ToString();
                    companyHistoryItem.Title = "" + item["Title"];
                    companyHistoryItem.Description = "" + item["Description"];
                    companyHistoryItem.PageTitle = "" + item["PageTitle"];
                    // companyHistoryItem.Date1 = "" + item["Date1"];
                    companyHistoryItem.Events = new List<CompanyHistoryEvent>();
                    for (int i = 1; i <= 6; i++)
                    {
                        CompanyHistoryEvent historyEvent = new CompanyHistoryEvent();
                        historyEvent.Date = "" + item["Date" + i];
                        historyEvent.Description = "" + item["Description" + i];
                        historyEvent.SmallPicture = "" + item["SmallPicture" + i];
                        historyEvent.LargePicture = "" + item["LargePicture" + i];
                    }
                }

                break;
            }

            return companyHistoryItem;
        }

        public static void GenerateJsonForCompanyHistory(SPWeb web, string[] paths)
        {
            SPList list = web.Lists["CompanyHistory"];
            Dictionary<string, List<CompanyHistoryItem>> dic = new Dictionary<string, List<CompanyHistoryItem>>();

            foreach (var path in paths)
            {
                dic.Add(path, new List<CompanyHistoryItem>());
            }
            foreach (SPListItem item in list.Items)
            {
                string key = "/" + item["Language"] + "/" + item["DeviceType"] + "/";
                if (dic.ContainsKey(key) && (!string.IsNullOrWhiteSpace("" + item["Title"])))
                {
                    CompanyHistoryItem companyHistoryItem = new CompanyHistoryItem();
                    companyHistoryItem.ID = item.ID.ToString();
                    companyHistoryItem.Title = "" + item["Title"];
                    companyHistoryItem.Description = "" + item["Description"];
                    companyHistoryItem.PageTitle = "" + item["PageTitle"];
                    // companyHistoryItem.Date1 = "" + item["Date1"];
                    companyHistoryItem.Events = new List<CompanyHistoryEvent>();
                    for(int i=1; i<=6; i++)
                    {
                        CompanyHistoryEvent historyEvent = new CompanyHistoryEvent();
                        historyEvent.Date = "" + item["Date" + i];
                        historyEvent.Description = "" + item["Description" + i];
                        historyEvent.SmallPicture = "" + item["SmallPicture" + i];
                        historyEvent.LargePicture = "" + item["LargePicture" + i];

                        companyHistoryItem.Events.Add(historyEvent);
                    }

                    dic[key].Add(companyHistoryItem);
                }

            }

            foreach (var key in dic.Keys)
            {
                if (dic[key].Count > 0)
                {
                    System.Web.Script.Serialization.JavaScriptSerializer jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                    var josnResult = "window.CompanyHistory =" + jsonSerializer.Serialize(dic[key]);
                    var path = web.Url + "/SiteData" + key + "CompanyHistory.json.js";
                    WriteFileToSharePointSite(web, path, josnResult);
                }
            }
        }

        // ContactUs
        public static ContactUsItem GetContactUsItem(SPWeb web, string language, string deveiceType)
        {
            SPList list = web.Lists["ContactUs"];
            string path = "/"+language + "/" + deveiceType + "/";
            ContactUsItem contactUsItem = null;
            foreach (SPListItem item in list.Items)
            {
                string key = "/" + item["Language"] + "/" + item["DeviceType"] + "/";
                if (key == path)
                {
                    contactUsItem = new ContactUsItem();
                    contactUsItem.ID = item.ID.ToString();
                    contactUsItem.Title = "" + item["Title"];
                    contactUsItem.Adress = "" + item["Adress"];
                    contactUsItem.PageTitle = "" + item["PageTitle"];
                    contactUsItem.BuildIcon = "" + item["BuildIcon"];
                    contactUsItem.CustomersIcon = "" + item["CustomersIcon"];
                    contactUsItem.CustomersLabel = "" + item["CustomersLabel"];
                    contactUsItem.CustomersMail = "" + item["CustomersMail"];
                    contactUsItem.FaxLabel = "" + item["FaxLabel"];
                    contactUsItem.FaxValue = "" + item["FaxValue"];
                    contactUsItem.JobSeekersIcon = "" + item["JobSeekersIcon"];
                    contactUsItem.JobSeekersLabel = "" + item["JobSeekersLabel"];
                    contactUsItem.JobSeekersMail = "" + item["JobSeekersMail"];
                    contactUsItem.MediaIcon = "" + item["MediaIcon"];
                    contactUsItem.MediaLabel = "" + item["MediaLabel"];
                    contactUsItem.MediaMail = "" + item["MediaMail"];
                    contactUsItem.PhoneLabel = "" + item["PhoneLabel"];
                    contactUsItem.PhoneValue = "" + item["PhoneValue"];


                    contactUsItem.SuppliersIcon = "" + item["SuppliersIcon"];
                    contactUsItem.SuppliersLabel = "" + item["SuppliersLabel"];
                    contactUsItem.SuppliersMail = "" + item["SuppliersMail"];
                }
            }

            return contactUsItem;
        }

        public static void GenerateJsonForContactUs(SPWeb web, string[] paths)
        {
            SPList list = web.Lists["ContactUs"];
            Dictionary<string, List<ContactUsItem>> dic = new Dictionary<string, List<ContactUsItem>>();
            foreach (var path in paths)
            {
                dic.Add(path, new List<ContactUsItem>());               
            }

            foreach (SPListItem item in list.Items)
            {
                string key = "/" + item["Language"] + "/" + item["DeviceType"] + "/";
                if (dic.ContainsKey(key) && (!string.IsNullOrWhiteSpace("" + item["Title"])))
                {
                    ContactUsItem contactUsItem = new ContactUsItem();
                    contactUsItem.ID = item.ID.ToString();
                    contactUsItem.Title = "" + item["Title"];
                    contactUsItem.Adress = "" + item["Adress"];
                    contactUsItem.PageTitle = "" + item["PageTitle"];
                    contactUsItem.BuildIcon = "" + item["BuildIcon"];
                    contactUsItem.CustomersIcon = "" + item["CustomersIcon"];
                    contactUsItem.CustomersLabel = "" + item["CustomersLabel"];
                    contactUsItem.CustomersMail = "" + item["CustomersMail"];
                    contactUsItem.FaxLabel = "" + item["FaxLabel"];
                    contactUsItem.FaxValue = "" + item["FaxValue"];
                    contactUsItem.JobSeekersIcon = "" + item["JobSeekersIcon"];
                    contactUsItem.JobSeekersLabel = "" + item["JobSeekersLabel"];
                    contactUsItem.JobSeekersMail = "" + item["JobSeekersMail"];
                    contactUsItem.MediaIcon = "" + item["MediaIcon"];
                    contactUsItem.MediaLabel = "" + item["MediaLabel"];
                    contactUsItem.MediaMail = "" + item["MediaMail"];
                    contactUsItem.PhoneLabel = "" + item["PhoneLabel"];
                    contactUsItem.PhoneValue = "" + item["PhoneValue"];


                    contactUsItem.SuppliersIcon = "" + item["SuppliersIcon"];
                    contactUsItem.SuppliersLabel = "" + item["SuppliersLabel"];
                    contactUsItem.SuppliersMail = "" + item["SuppliersMail"];
                   
                    dic[key].Add(contactUsItem);
                }
            }

            foreach (var key in dic.Keys)
            {
                if (dic[key].Count > 0)
                {
                    System.Web.Script.Serialization.JavaScriptSerializer jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                    var josnResult = "window.ContactUs =" + jsonSerializer.Serialize(dic[key]);
                    var path = web.Url + "/SiteData" + key + "ContactUs.json.js";
                    WriteFileToSharePointSite(web, path, josnResult);
                }
            }
        }

        //Get LeaderShips
        public static LeaderShipMainItem GetLeaderShips(SPWeb web, string language, string deveiceType)
        {
            SPList list = web.Lists["LeaderShipMain"];
            string path = "/"+language + "/" + deveiceType + "/";
            LeaderShipMainItem leaderShip = null;
            foreach (SPListItem item in list.Items)
            {
                string key = "/" + item["Language"] + "/" + item["DeviceType"] + "/";
                if (path == key)
                {
                    leaderShip = new LeaderShipMainItem();
                    leaderShip.ID = item.ID.ToString();
                    leaderShip.Title = "" + item["Title"];
                    leaderShip.PageTitle = "" + item["PageTitle"];
                    leaderShip.Leaders = new List<LeaderShipDetailItem>();
                    SPList subList = web.Lists["LeaderShipDetail"];

                    foreach (SPListItem subItem in subList.Items)
                    {
                        string subKey = "/" + subItem["Language"] + "/" + subItem["DeviceType"] + "/";
                        if (subKey == key)
                        {
                            LeaderShipDetailItem leaderShipDetailItem = new LeaderShipDetailItem();
                            leaderShipDetailItem.Title = "" + subItem["Title"];
                            leaderShipDetailItem.Name = "" + subItem["Name"];
                            leaderShipDetailItem.ID = subItem.ID.ToString();
                            leaderShipDetailItem.Introduction = "" + subItem["Introduction"];
                            leaderShipDetailItem.Position = "" + subItem["Position"];
                            leaderShipDetailItem.Picture = "" + subItem["Picture"];
                            leaderShipDetailItem.Order = subItem["Order0"] == null ? 99999 : int.Parse("" + subItem["Order0"]);
                            leaderShip.Leaders.Add(leaderShipDetailItem);
                        }
                    }

                    leaderShip.Leaders = leaderShip.Leaders.OrderBy(t => t.Order).ToList();

                    break;
                }
            }

            return leaderShip;
        }

       public static void GenerateJsonForLeaderShip(SPWeb web, string[] paths)
       {
           SPList list = web.Lists["LeaderShipMain"];
           Dictionary<string, List<LeaderShipMainItem>> dic = new Dictionary<string, List<LeaderShipMainItem>>();
           foreach (var path in paths)
           {
               dic.Add(path, new List<LeaderShipMainItem>());
           }

           foreach (SPListItem item in list.Items)
           {
               string key = "/" + item["Language"] + "/" + item["DeviceType"] + "/";
               if (dic.ContainsKey(key) && (!string.IsNullOrWhiteSpace("" + item["Title"])))
               {
                   LeaderShipMainItem leaderShipMainItem = new LeaderShipMainItem();
                   leaderShipMainItem.ID = item.ID.ToString();
                   leaderShipMainItem.Title = "" + item["Title"];
                   leaderShipMainItem.PageTitle = "" + item["PageTitle"];
                   leaderShipMainItem.Leaders = new List<LeaderShipDetailItem>();
                   SPList subList = web.Lists["LeaderShipDetail"];
                   foreach (SPListItem subItem in subList.Items)
                   {
                       string subKey = "/" + subItem["Language"] + "/" + subItem["DeviceType"] + "/";
                       if (subKey == key)
                       {
                           LeaderShipDetailItem leaderShipDetailItem = new LeaderShipDetailItem();
                           leaderShipDetailItem.Title = "" + subItem["Title"];
                           leaderShipDetailItem.Name = "" + subItem["Name"];
                           leaderShipDetailItem.ID = subItem.ID.ToString();
                           leaderShipDetailItem.Introduction = "" + subItem["Introduction"];
                           leaderShipDetailItem.Position = "" + subItem["Position"];
                           leaderShipDetailItem.Picture = "" + subItem["Picture"];
                           leaderShipDetailItem.Order = subItem["Order0"] == null ? 99999 : int.Parse("" + subItem["Order0"]);
                           leaderShipMainItem.Leaders.Add(leaderShipDetailItem);
                       }
                   }

                   leaderShipMainItem.Leaders = leaderShipMainItem.Leaders.OrderBy(t => t.Order).ToList();

                   dic[key].Add(leaderShipMainItem);
               }
           }

           foreach (var key in dic.Keys)
           {
               if (dic[key].Count > 0)
               {
                   System.Web.Script.Serialization.JavaScriptSerializer jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                   var josnResult = "window.LeaderShip =" + jsonSerializer.Serialize(dic[key]);
                   var path = web.Url + "/SiteData" + key + "LeaderShip.json.js";
                   WriteFileToSharePointSite(web, path, josnResult);
               }
           }
       }

        // PartnerShip
       public static PartnerShipItem GetPartnerShipItem(SPWeb web, string language, string deviceType)
       {
           string path = "/" + language + "/" + deviceType + "/";
           SPList list = web.Lists["PartnerShip"];
           PartnerShipItem partnerShipItem = null;
           foreach (SPListItem item in list.Items)
           {
               string key = "/" + item["Language"] + "/" + item["DeviceType"] + "/";
               if (key == path)
               {
                   partnerShipItem = new PartnerShipItem();
                   partnerShipItem.ID = item.ID.ToString();
                   partnerShipItem.Title = "" + item["Title"];

                   partnerShipItem.BottomDescription = "" + item["BottomDescription"];
                   partnerShipItem.Left1 = "" + item["Left1"];
                   partnerShipItem.Left2 = "" + item["Left2"];
                   partnerShipItem.Left3 = "" + item["Left3"];
                   partnerShipItem.Right1 = "" + item["Right1"];
                   partnerShipItem.Right2 = "" + item["Right2"];
                   partnerShipItem.Right3 = "" + item["Right3"];
                   partnerShipItem.TopDescription = "" + item["TopDescription"];
                   partnerShipItem.LeftTitle = "" + item["LeftTitle"];
                   partnerShipItem.RightTitle = "" + item["RightTitle"];
                   partnerShipItem.PageTitle = "" + item["PageTitle"];
                   break;
               }
           }

           return partnerShipItem;
       }

       public static void GenerateJsonForPartnerShip(SPWeb web, string[] paths)
       {
           SPList list = web.Lists["PartnerShip"];
           Dictionary<string, List<PartnerShipItem>> dic = new Dictionary<string, List<PartnerShipItem>>();
           foreach (var path in paths)
           {
               dic.Add(path, new List<PartnerShipItem>());
           }

           foreach (SPListItem item in list.Items)
           {
               string key = "/" + item["Language"] + "/" + item["DeviceType"] + "/";
               if (dic.ContainsKey(key) && (!string.IsNullOrWhiteSpace("" + item["Title"])))
               {
                   PartnerShipItem partnerShipItem = new PartnerShipItem();
                   partnerShipItem.ID = item.ID.ToString();
                   partnerShipItem.Title = "" + item["Title"];

                   partnerShipItem.BottomDescription = "" + item["BottomDescription"];
                   partnerShipItem.Left1 = "" + item["Left1"];
                   partnerShipItem.Left2 = "" + item["Left2"];
                   partnerShipItem.Left3 = "" + item["Left3"];
                   partnerShipItem.Right1 = "" + item["Right1"];
                   partnerShipItem.Right2 = "" + item["Right2"];
                   partnerShipItem.Right3 = "" + item["Right3"];
                   partnerShipItem.TopDescription = "" + item["TopDescription"];
                   partnerShipItem.LeftTitle = "" + item["LeftTitle"];
                   partnerShipItem.RightTitle = "" + item["RightTitle"];
                   partnerShipItem.PageTitle = "" + item["PageTitle"];
                   dic[key].Add(partnerShipItem);
               }
           }


           foreach (var key in dic.Keys)
           {
               if (dic[key].Count > 0)
               {
                   System.Web.Script.Serialization.JavaScriptSerializer jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                   var josnResult = "window.PartnerShip =" + jsonSerializer.Serialize(dic[key]);
                   var path = web.Url + "/SiteData" + key + "PartnerShip.json.js";
                   WriteFileToSharePointSite(web, path, josnResult);
               }
           }
       }

        // SocialDutyItem
       public static SocialDutyItem GetSocialDutyItem(SPWeb web, string language, string deviceType)
       {
           SPList list = web.Lists["SocialDuty"];
           SocialDutyItem socialDutyItem = null;
           string path = "/" + language + "/" + deviceType + "/";
           foreach (SPListItem item in list.Items)
           {
               string key = "/" + item["Language"] + "/" + item["DeviceType"] + "/";
               if (key == path)
               {
                   socialDutyItem = new SocialDutyItem();
                   socialDutyItem.ID = item.ID.ToString();
                   socialDutyItem.Description = "" + item["Description"];
                   socialDutyItem.Picture1 = "" + item["Picture1"];
                   socialDutyItem.Picture2 = "" + item["Picture2"];
                   socialDutyItem.Picture3 = "" + item["Picture3"];
                   socialDutyItem.Picture4 = "" + item["Picture4"];
                   socialDutyItem.Picture5 = "" + item["Picture5"];
                   socialDutyItem.Picture6 = "" + item["Picture6"];
                   socialDutyItem.Picture7 = "" + item["Picture7"];
                   socialDutyItem.Picture8 = "" + item["Picture8"];
                   socialDutyItem.Title = "" + item["Title"];
                   socialDutyItem.PageTitle = "" + item["PageTitle"];
                   break;
               }
           }

           return socialDutyItem;
       }

       public static void GenerateJsonForSocialDuty(SPWeb web, string[] paths)
       {
           SPList list = web.Lists["SocialDuty"];
           Dictionary<string, List<SocialDutyItem>> dic = new Dictionary<string, List<SocialDutyItem>>();
           foreach (var path in paths)
           {
               dic.Add(path, new List<SocialDutyItem>());
           }
           foreach (SPListItem item in list.Items)
           {
               string key = "/" + item["Language"] + "/" + item["DeviceType"] + "/";
               if (dic.ContainsKey(key) && (!string.IsNullOrWhiteSpace("" + item["Title"])))
               {
                   SocialDutyItem socialDutyItem = new SocialDutyItem();
                   socialDutyItem.ID = item.ID.ToString();
                   socialDutyItem.Description = "" + item["Description"];
                   socialDutyItem.Picture1 = "" + item["Picture1"];
                   socialDutyItem.Picture2 = "" + item["Picture2"];
                   socialDutyItem.Picture3 = "" + item["Picture3"];
                   socialDutyItem.Picture4 = "" + item["Picture4"];
                   socialDutyItem.Picture5 = "" + item["Picture5"];
                   socialDutyItem.Picture6 = "" + item["Picture6"];
                   socialDutyItem.Picture7 = "" + item["Picture7"];
                   socialDutyItem.Picture8 = "" + item["Picture8"];
                   socialDutyItem.Title = "" + item["Title"];
                   socialDutyItem.PageTitle = "" + item["PageTitle"];
                   dic[key].Add(socialDutyItem);
               }
           }


           foreach (var key in dic.Keys)
           {
               if (dic[key].Count > 0)
               {
                   System.Web.Script.Serialization.JavaScriptSerializer jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                   var josnResult = "window.CitizenShip =" + jsonSerializer.Serialize(dic[key]);
                   var path = web.Url + "/SiteData" + key + "CitizenShip.json.js";
                   WriteFileToSharePointSite(web, path, josnResult);
               }
           }
       }

        // CareerItem
       public static CareerItem GetCareerItem(SPWeb web, string language, string deviceType)
       {
           string path = "/" + language + "/" + deviceType + "/";
           SPList list = web.Lists["Career"];
           CareerItem careerItem  = null;
           foreach (SPListItem item in list.Items)
           {
               string key = "/" + item["Language"] + "/" + item["DeviceType"] + "/";
               if (path == key)
               {
                   careerItem = new CareerItem();
                   careerItem.ID = item.ID.ToString();
                   careerItem.Title = "" + item["Title"];
                   careerItem.RightPicture = "" + item["RightPicture"];
                   careerItem.Description = "" + item["Description"];
                   careerItem.CareerPathPicture = "" + item["CareerPathPicture"];
                   careerItem.CareerPath3 = "" + item["CareerPath3"];
                   careerItem.CareerPath2 = "" + item["CareerPath2"];
                   careerItem.CareerPath1 = "" + item["CareerPath1"];
                   careerItem.CareerOpportunitiesLink = "" + item["CareerOpportunitiesLink"];
                   careerItem.CareerOpportunities = "" + item["CareerOpportunities"];
                   careerItem.PageTitle = "" + item["PageTitle"];

                   break;
               }
           }

           return careerItem;
       }


       public static void GenerateJsonForCareer(SPWeb web, string[] paths)
       {
           SPList list = web.Lists["Career"];
           Dictionary<string, List<CareerItem>> dic = new Dictionary<string, List<CareerItem>>();
           foreach (var path in paths)
           {
               dic.Add(path, new List<CareerItem>());
           }
           foreach (SPListItem item in list.Items)
           {
               string key = "/" + item["Language"] + "/" + item["DeviceType"] + "/";
               if (dic.ContainsKey(key) && (!string.IsNullOrWhiteSpace("" + item["Title"])))
               {
                   CareerItem careerItem = new CareerItem();
                   careerItem.ID = item.ID.ToString();
                   careerItem.Title = "" + item["Title"];
                   careerItem.RightPicture = "" + item["RightPicture"];
                   careerItem.Description = "" + item["Description"];
                   careerItem.CareerPathPicture = "" + item["CareerPathPicture"];
                   careerItem.CareerPath3 = "" + item["CareerPath3"];
                   careerItem.CareerPath2 = "" + item["CareerPath2"];
                   careerItem.CareerPath1 = "" + item["CareerPath1"];
                   careerItem.CareerOpportunitiesLink = "" + item["CareerOpportunitiesLink"];
                   careerItem.CareerOpportunities = "" + item["CareerOpportunities"];
                   careerItem.PageTitle = "" + item["PageTitle"];
                   dic[key].Add(careerItem);
               }
           }

           foreach (var key in dic.Keys)
           {
               if (dic[key].Count > 0)
               {
                   System.Web.Script.Serialization.JavaScriptSerializer jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                   var josnResult = "window.Career =" + jsonSerializer.Serialize(dic[key]);
                   var path = web.Url + "/SiteData" + key + "Career.json.js";
                   WriteFileToSharePointSite(web, path, josnResult);
               }
           }

       }

        // TrainingItem
       public static TrainingItem GetTrainingItem(SPWeb web, string language, string deviceType)
       {
           string path = "/" + language + "/" + deviceType + "/";
           TrainingItem trainingItem = null;
           SPList list = web.Lists["Training"];
           foreach (SPListItem item in list.Items)
           {
               string key = "/" + item["Language"] + "/" + item["DeviceType"] + "/";
               if (key == path)
               {
                   trainingItem = new TrainingItem();
                   trainingItem.ID = item.ID.ToString();
                   trainingItem.Title = "" + item["Title"];
                   trainingItem.Description = "" + item["Description"];
                   trainingItem.Picture = "" + item["Picture"];
                   trainingItem.PageTitle = "" + item["PageTitle"];

                   break;
               }              
           }

           return trainingItem;
       }

       public static void GenerateJsonForTraining(SPWeb web, string[] paths)
       {
           SPList list = web.Lists["Training"];
           Dictionary<string, List<TrainingItem>> dic = new Dictionary<string, List<TrainingItem>>();
           foreach (var path in paths)
           {
               dic.Add(path, new List<TrainingItem>());
           }

           foreach (SPListItem item in list.Items)
           {
               string key = "/" + item["Language"] + "/" + item["DeviceType"] + "/";
               if (dic.ContainsKey(key) && (!string.IsNullOrWhiteSpace("" + item["Title"])))
               {
                   TrainingItem trainingItem = new TrainingItem();
                   trainingItem.ID = item.ID.ToString();
                   trainingItem.Title = "" + item["Title"];
                   trainingItem.Description = "" + item["Description"];
                   trainingItem.Picture = "" + item["Picture"];
                   trainingItem.PageTitle = "" + item["PageTitle"];
                   dic[key].Add(trainingItem);
               }
           }

           foreach (var key in dic.Keys)
           {
               if (dic[key].Count > 0)
               {
                   System.Web.Script.Serialization.JavaScriptSerializer jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                   var josnResult = "window.Training =" + jsonSerializer.Serialize(dic[key]);
                   var path = web.Url + "/SiteData" + key + "Training.json.js";
                   WriteFileToSharePointSite(web, path, josnResult);
               }
           }

       }

        // OurLifeItem
       public static OurLifeItem GetOurLifeItem(SPWeb web, string language, string deviceType)
       {
           string path = "/" + language + "/" + deviceType + "/";
           OurLifeItem ourLifeItem = null;
           SPList list = web.Lists["OurLife"];
           foreach (SPListItem item in list.Items)
           {
               string key = "/" + item["Language"] + "/" + item["DeviceType"] + "/";
               if (key == path)
               {
                   ourLifeItem = new OurLifeItem();
                   ourLifeItem.Title = "" + item["Title"];
                   ourLifeItem.Description = "" + item["Description"];
                   string pictures = "" + item["Pictures"];
                   string[] picArray = pictures.Split("\n".ToArray(), StringSplitOptions.RemoveEmptyEntries);
                   ourLifeItem.Pictures = new List<string>(picArray);
                   break;
               }
           }

           return ourLifeItem;
       }

       public static void GenerateJsonForOurLife(SPWeb web, string[] paths)
       {
           SPList list = web.Lists["OurLife"];
           Dictionary<string, List<OurLifeItem>> dic = new Dictionary<string, List<OurLifeItem>>();
           foreach (var path in paths)
           {
               dic.Add(path, new List<OurLifeItem>());
           }

           foreach (SPListItem item in list.Items)
           {
               string key = "/" + item["Language"] + "/" + item["DeviceType"] + "/";
               if (dic.ContainsKey(key) && (!string.IsNullOrWhiteSpace("" + item["Title"])))
               {
                   OurLifeItem ourLifeItem = new OurLifeItem();
                   ourLifeItem.Title = "" + item["Title"];
                   ourLifeItem.Description = "" + item["Description"];
                   string pictures = "" + item["Pictures"];
                   string[] picArray = pictures.Split("\n".ToArray(), StringSplitOptions.RemoveEmptyEntries);
                   ourLifeItem.Pictures = new List<string>(picArray);
                   dic[key].Add(ourLifeItem);
               }
           }
           foreach (var key in dic.Keys)
           {
               if (dic[key].Count > 0)
               {
                   System.Web.Script.Serialization.JavaScriptSerializer jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                   var josnResult = "window.OurLife =" + jsonSerializer.Serialize(dic[key]);
                   var path = web.Url + "/SiteData" + key + "OurLife.json.js";
                   WriteFileToSharePointSite(web, path, josnResult);
               }
           }
       }

        // OpportunitiesItem
       public static OpportunitiesItem GetOpportunitiesItem(SPWeb web, string language, string deviceType)
       {
           string path = "/" + language + "/" + deviceType + "/";
           SPList list = web.Lists["Opportunities"];
           OpportunitiesItem opportunitiesItem = null;

           foreach (SPListItem item in list.Items)
           {
               string key = "/" + item["Language"] + "/" + item["DeviceType"] + "/";
               if (path == key)
               {
                   opportunitiesItem = new OpportunitiesItem();
                   opportunitiesItem.ID = item.ID.ToString();
                   opportunitiesItem.Title = "" + item["Title"];
                   opportunitiesItem.JobTitle2 = "" + item["JobTitle2"];
                   opportunitiesItem.JobTitle1 = "" + item["JobTitle1"];
                   opportunitiesItem.JobDescription2 = "" + item["JobDescription2"];
                   opportunitiesItem.JobDescription1 = "" + item["JobDescription1"];
                   opportunitiesItem.HotPositionLabel = "" + item["HotPositionLabel"];
                   opportunitiesItem.HotPositionIcon = "" + item["HotPositionIcon"];
                   opportunitiesItem.Description = "" + item["Description"];
                   opportunitiesItem.CheckAllJobLink = "" + item["CheckAllJobLink"];
                   opportunitiesItem.CheckAllJobLabel = "" + item["CheckAllJobLabel"];
                   opportunitiesItem.PageTitle = "" + item["PageTitle"];
                   opportunitiesItem.BackgroundPicture =item["BackgroundPicture"] == null? "/_catalogs/masterpage/ASDesktop/img/cr0113.png" : "" +item["BackgroundPicture"];              
                   break;
               }
           }

           return opportunitiesItem;
       }

       public static void GenerateJsonForOpportunities(SPWeb web, string[] paths)
       {
           SPList list = web.Lists["Opportunities"];
           Dictionary<string, List<OpportunitiesItem>> dic = new Dictionary<string, List<OpportunitiesItem>>();
           foreach (var path in paths)
           {
               dic.Add(path, new List<OpportunitiesItem>());
           }

           foreach (SPListItem item in list.Items)
           {
               string key = "/" + item["Language"] + "/" + item["DeviceType"] + "/";
               if (dic.ContainsKey(key) && (!string.IsNullOrWhiteSpace("" + item["Title"])))
               {
                   OpportunitiesItem opportunitiesItem = new OpportunitiesItem();
                   opportunitiesItem.ID = item.ID.ToString();
                   opportunitiesItem.Title = "" + item["Title"];
                   opportunitiesItem.JobTitle2 = "" + item["JobTitle2"];
                   opportunitiesItem.JobTitle1 = "" + item["JobTitle1"];
                   opportunitiesItem.JobDescription2 = "" + item["JobDescription2"];
                   opportunitiesItem.JobDescription1 = "" + item["JobDescription1"];
                   opportunitiesItem.HotPositionLabel = "" + item["HotPositionLabel"];
                   opportunitiesItem.HotPositionIcon = "" + item["HotPositionIcon"];
                   opportunitiesItem.Description = "" + item["Description"];
                   opportunitiesItem.CheckAllJobLink = "" + item["CheckAllJobLink"];
                   opportunitiesItem.CheckAllJobLabel = "" + item["CheckAllJobLabel"];
                   opportunitiesItem.PageTitle = "" + item["PageTitle"];
                   opportunitiesItem.BackgroundPicture = item["BackgroundPicture"] == null ? "/_catalogs/masterpage/ASDesktop/img/cr0113.png" : "" + item["BackgroundPicture"];              
                   dic[key].Add(opportunitiesItem);
               }
           }

           foreach (var key in dic.Keys)
           {
               if (dic[key].Count > 0)
               {
                   System.Web.Script.Serialization.JavaScriptSerializer jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                   var josnResult = "window.Opportunities =" + jsonSerializer.Serialize(dic[key]);
                   var path = web.Url + "/SiteData" + key + "Opportunities.json.js";
                   WriteFileToSharePointSite(web, path, josnResult);
               }
           }
       }

        // NewsMainItem
       public static List<NewsDetailItem> GetNewsDetailItems(SPWeb web, string language, string deveiceType)
       {
           SPList newsDetailList = web.Lists["NewsDetail"];
           string path = "/" + language + "/" + deveiceType + "/";
           List<NewsDetailItem> newsDetailItems =  new List<NewsDetailItem>();

           foreach (SPListItem subItem in newsDetailList.Items)
           {
               string key = "/" + subItem["Language"] + "/" + subItem["DeviceType"] + "/";
               if (path == key)
               {
                   NewsDetailItem newsDetailItem = new NewsDetailItem();
                   newsDetailItem.Title = "" + subItem["Title"];
                   newsDetailItem.ID = subItem.ID.ToString();
                   newsDetailItem.SubTitle = "" + subItem["SubTitle"];
                   newsDetailItem.Order = subItem["Order0"] == null ? 99999 : int.Parse("" + subItem["Order0"]);
                   newsDetailItem.Date = subItem["Date"] == null ? null : (DateTime?)subItem["Date"];
                   newsDetailItem.Content = "" + subItem["Content"];
                   newsDetailItem.CompanyAboutTitle = "" + subItem["CompanyAboutTitle"];
                   newsDetailItem.CompanyAboutContent = "" + subItem["CompanyAboutContent"];
                   newsDetailItem.Picture = "" + subItem["Picture"];

                   if ("" + subItem["Language"] == "English")
                   {
                       newsDetailItem.URL = "/pages/NewsDetail.aspx?newsid=" + newsDetailItem.ID;
                   }
                   else if ("" + subItem["Language"] == "Chinese")
                   {
                       newsDetailItem.URL = "/pages/NewsDetail_cn.aspx?newsid=" + newsDetailItem.ID;
                   }

                   newsDetailItem.ShortDescription = "" + subItem["ShortDescription"];
                   newsDetailItems.Add(newsDetailItem);
               }
           }

           return newsDetailItems;
       }


       public static NewsMainItem GetNewsMainItem(SPWeb web, string language, string deveiceType)
       {
           string path = "/" + language + "/" + deveiceType + "/";
           SPList newsMainList = web.Lists["NewsMain"];
           NewsMainItem newsMainItem = null;
           foreach (SPListItem item in newsMainList.Items)
           {
               string key = "/" + item["Language"] + "/" + item["DeviceType"] + "/";
               if (key == path)
               {
                   newsMainItem = new NewsMainItem();
                   newsMainItem.ID = item.ID.ToString();
                   newsMainItem.Title = "" + item["Title"];
                   newsMainItem.ContactInfo = "" + item["ContactInfo"];
                   newsMainItem.MediaQueryContent = "" + item["MediaQueryContent"];
                   newsMainItem.MediaQueryTitle = "" + item["MediaQueryTitle"];
                   newsMainItem.NewsItems = new List<NewsDetailItem>();
                   newsMainItem.SubscribeLabel = "" + item["SubscribeLabel"];
                   newsMainItem.SubscribeLink = "" + item["SubscribeLink"];
                   newsMainItem.ViewMoreLabel = "" + item["ViewMoreLabel"];
                   newsMainItem.PageTitle = "" + item["PageTitle"];
               }
           }

           return newsMainItem;
       }

       public static void GenerateJsonForNewsMain(SPWeb web, string[] paths)
       {
           SPList list = web.Lists["NewsMain"];
           Dictionary<string, List<NewsMainItem>> dic = new Dictionary<string, List<NewsMainItem>>();
           foreach (var path in paths)
           {
               dic.Add(path, new List<NewsMainItem>());
           }
           foreach (SPListItem item in list.Items)
           {
               string key = "/" + item["Language"] + "/" + item["DeviceType"] + "/";
               if (dic.ContainsKey(key) && (!string.IsNullOrWhiteSpace("" + item["Title"])))
               {
                   // Todo
                   NewsMainItem newsMainItem = new NewsMainItem();
                   newsMainItem.ID = item.ID.ToString();
                   newsMainItem.Title = "" + item["Title"];
                   newsMainItem.ContactInfo = "" + item["ContactInfo"];
                   newsMainItem.MediaQueryContent = "" + item["MediaQueryContent"];
                   newsMainItem.MediaQueryTitle = "" + item["MediaQueryTitle"];
                   newsMainItem.NewsItems = new List<NewsDetailItem>();
                   newsMainItem.SubscribeLabel = "" + item["SubscribeLabel"];
                   newsMainItem.SubscribeLink = "" + item["SubscribeLink"];
                   newsMainItem.ViewMoreLabel = "" + item["ViewMoreLabel"];
                   newsMainItem.PageTitle = "" + item["PageTitle"];
                   SPList newsDetailList = web.Lists["NewsDetail"];
                   foreach (SPListItem subItem in newsDetailList.Items)
                   {
                        string subKey = "/" + subItem["Language"] + "/" + subItem["DeviceType"] + "/";
                        if (subKey == key)
                        {
                            NewsDetailItem newsDetailItem = new NewsDetailItem();
                            newsDetailItem.Title = "" + subItem["Title"];
                            newsDetailItem.ID = subItem.ID.ToString();
                            newsDetailItem.SubTitle = "" + subItem["SubTitle"];
                            newsDetailItem.Order = subItem["Order0"] == null ? 99999 : int.Parse("" + subItem["Order0"]);
                            newsDetailItem.Date = subItem["Date"] == null ? null : (DateTime ?)subItem["Date"];
                            newsDetailItem.Content = "" + subItem["Content"];
                            newsDetailItem.CompanyAboutTitle = "" + subItem["CompanyAboutTitle"];
                            newsDetailItem.CompanyAboutContent = "" + subItem["CompanyAboutContent"];
                            newsDetailItem.Picture = "" + subItem["Picture"];

                            if ("" + subItem["Language"] == "English")
                            {
                                newsDetailItem.URL = "/pages/NewsDetail.aspx?newsid=" + newsDetailItem.ID;
                            }
                            else if ("" + subItem["Language"] == "Chinese")
                            {
                                newsDetailItem.URL = "/pages/NewsDetail_cn.aspx?newsid=" + newsDetailItem.ID;
                            }

                            newsDetailItem.ShortDescription = "" +subItem["ShortDescription"]; 
                            newsMainItem.NewsItems.Add(newsDetailItem);
                        }
                   }

                   newsMainItem.NewsItems = newsMainItem.NewsItems.OrderBy(t => t.Order).ToList();
                   dic[key].Add(newsMainItem);
               }
           }

           foreach (var key in dic.Keys)
           {
               if (dic[key].Count > 0)
               {
                   System.Web.Script.Serialization.JavaScriptSerializer jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                   var josnResult = "window.News =" + jsonSerializer.Serialize(dic[key]);
                   var path = web.Url + "/SiteData" + key + "News.json.js";
                   WriteFileToSharePointSite(web, path, josnResult);
               }
           }
       }

        // MediaCenterItem
       public static MediaCenterItem GetMediaCenterItem(SPWeb web, string language, string deveiceType)
       {
           string path = "/" + language + "/" + deveiceType + "/";
           SPList list = web.Lists["MediaCenter"];
           MediaCenterItem mediaCenterItem = null;
           foreach (SPListItem item in list.Items)
           {
               string key = "/" + item["Language"] + "/" + item["DeviceType"] + "/";
               if (key == path)
               {
                   mediaCenterItem = new MediaCenterItem();
                   mediaCenterItem.Title = "" + item["Title"];
                   mediaCenterItem.PageTitle = "" + item["PageTitle"];
                   mediaCenterItem.MediaItems = new List<MediaDetailItem>();
                   SPList mediaDetailList = web.Lists["MediaDetail"];
                   foreach (SPListItem subItem in mediaDetailList.Items)
                   {
                       string subKey = "/" + subItem["Language"] + "/" + subItem["DeviceType"] + "/";
                       if (subKey == key)
                       {
                           MediaDetailItem mediaDetailItem = new MediaDetailItem();
                           mediaDetailItem.ID = subItem.ID.ToString(); //Add by leiz
                           mediaDetailItem.Title = "" + subItem["Title"];
                           mediaDetailItem.Picture = "" + subItem["Picture"];
                           mediaDetailItem.Order = subItem["Order0"] == null ? 99999 : int.Parse("" + subItem["Order0"]);
                           mediaDetailItem.Description = "" + subItem["Description"];
                           mediaDetailItem.MeidaUrl = new List<string>(("" + subItem["MeidaUrl"]).Split("\n".ToCharArray()));
                           mediaCenterItem.MediaItems.Add(mediaDetailItem);
                       }

                       mediaCenterItem.MediaItems = mediaCenterItem.MediaItems.OrderBy(x => x.Order).ToList();
                   }

                   break;
               }
           }

           return mediaCenterItem;
       }

       public static void GenerateJsonForMediaCenter(SPWeb web, string[] paths)
       {
           SPList list = web.Lists["MediaCenter"];
           Dictionary<string, List<MediaCenterItem>> dic = new Dictionary<string, List<MediaCenterItem>>();
           foreach (var path in paths)
           {
               dic.Add(path, new List<MediaCenterItem>());
           }
           foreach (SPListItem item in list.Items)
           {
               string key = "/" + item["Language"] + "/" + item["DeviceType"] + "/";
               if (dic.ContainsKey(key) && (!string.IsNullOrWhiteSpace("" + item["Title"])))
               {
                   MediaCenterItem mediaCenterItem = new MediaCenterItem();
                   mediaCenterItem.Title = ""+ item["Title"];
                   mediaCenterItem.PageTitle = "" + item["PageTitle"];
                   mediaCenterItem.MediaItems = new List<MediaDetailItem>();

                   SPList mediaDetailList = web.Lists["MediaDetail"];
                   foreach (SPListItem subItem in mediaDetailList.Items)
                   {
                       string subKey = "/" + subItem["Language"] + "/" + subItem["DeviceType"] + "/";
                       if (subKey == key)
                       {
                           MediaDetailItem mediaDetailItem = new MediaDetailItem();
                           mediaDetailItem.Title = "" + subItem["Title"];
                           mediaDetailItem.Picture = "" + subItem["Picture"];
                           mediaDetailItem.Order = subItem["Order0"] == null ? 99999 : int.Parse("" + subItem["Order0"]);
                           mediaDetailItem.Description = "" + subItem["Description"];
                           mediaDetailItem.MeidaUrl = new List<string>(("" + subItem["MeidaUrl"]).Split("\n".ToCharArray()));
                           mediaCenterItem.MediaItems.Add(mediaDetailItem);
                       }

                       mediaCenterItem.MediaItems = mediaCenterItem.MediaItems.OrderBy(x => x.Order).ToList();
                   }


                   dic[key].Add(mediaCenterItem);
               }
           }


           foreach (var key in dic.Keys)
           {
               if (dic[key].Count > 0)
               {
                   System.Web.Script.Serialization.JavaScriptSerializer jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                   var josnResult = "window.Media =" + jsonSerializer.Serialize(dic[key]);
                   var path = web.Url + "/SiteData" + key + "Media.json.js";
                   WriteFileToSharePointSite(web, path, josnResult);
               }
           }
       }


        // ServiceItem
       public static ServiceItem GetServiceItem(SPWeb web, string language, string deveiceType)
       {
           string path = "/" + language + "/" + deveiceType + "/";
           SPList list = web.Lists["Service"];
           ServiceItem serviceItem = null;
           foreach (SPListItem item in list.Items)
           {
               string key = "/" + item["Language"] + "/" + item["DeviceType"] + "/";
               if (path == key)
               {
                   serviceItem = new ServiceItem();
                   serviceItem.ID = item.ID.ToString();
                   serviceItem.Title = "" + item["Title"];
                   serviceItem.Content = "" + item["Content"];
                   serviceItem.PageTitle = "" + item["PageTitle"];
                   serviceItem.BackgroundPicture = item["BackgroundPicture"] == null ? "/_catalogs/masterpage/ASDesktop/img/service1.png" : "" + item["BackgroundPicture"];
                   break;
               }
           }

           return serviceItem;
       }

       public static void GenerateJsonForService(SPWeb web, string[] paths)
       {
           SPList list = web.Lists["Service"];
           Dictionary<string, List<ServiceItem>> dic = new Dictionary<string, List<ServiceItem>>();
           foreach (var path in paths)
           {
               dic.Add(path, new List<ServiceItem>());
           }

           foreach (SPListItem item in list.Items)
           {
               string key = "/" + item["Language"] + "/" + item["DeviceType"] + "/";
               if (dic.ContainsKey(key) && (!string.IsNullOrWhiteSpace("" + item["Title"])))
               {
                   ServiceItem serviceItem = new ServiceItem();
                   serviceItem.ID = item.ID.ToString();
                   serviceItem.Title = "" + item["Title"];
                   serviceItem.Content = "" + item["Content"];
                   serviceItem.PageTitle = "" + item["PageTitle"];
                   serviceItem.BackgroundPicture = item["BackgroundPicture"] == null ? "/_catalogs/masterpage/ASDesktop/img/service1.png" : "" + item["BackgroundPicture"];
                  //  serviceItem.OriginalBackgroundPicture = item["OriginalBackgroundPicture"] == null ? "/_catalogs/masterpage/ASDesktop/img/service1.png" : "" + item["OriginalBackgroundPicture"];
                   dic[key].Add(serviceItem);
               }
           }

           foreach (var key in dic.Keys)
           {
               if (dic[key].Count > 0)
               {
                   System.Web.Script.Serialization.JavaScriptSerializer jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                   var josnResult = "window.Service =" + jsonSerializer.Serialize(dic[key]);
                   var path = web.Url + "/SiteData" + key + "Service.json.js";
                   WriteFileToSharePointSite(web, path, josnResult);
               }
           }
       }

       public static void GenerateJsonForTerms(SPWeb web, string[] paths)
       {
           SPList list = web.Lists["Terms"];
           Dictionary<string, List<Terms>> dic = new Dictionary<string, List<Terms>>();
           foreach (var path in paths)
           {
               dic.Add(path, new List<Terms>());
           }

          foreach (SPListItem item in list.Items)
           {
               string key = "/" + item["Language"] + "/" + item["DeviceType"] + "/";
               if (dic.ContainsKey(key) && (!string.IsNullOrWhiteSpace("" + item["Title"])))
               {
                   Terms termItem = new Terms();
                   termItem.ID = item.ID.ToString();
                   termItem.PageTitle = "" + item["PageTitle"];
                   termItem.ContentHtml = "" + item["ContentHtml"];
                   dic[key].Add(termItem);
               }
           }

          foreach (var key in dic.Keys)
          {
              if (dic[key].Count > 0)
              {
                  System.Web.Script.Serialization.JavaScriptSerializer jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                  var josnResult = "window.Terms =" + jsonSerializer.Serialize(dic[key]);
                  var path = web.Url + "/SiteData" + key + "Terms.json.js";
                  WriteFileToSharePointSite(web, path, josnResult);
              }
          }
       }
     

       public static void GenerateJsonForSiteMap(SPWeb web, string[] paths)
       {
           SPList list = web.Lists["SiteMap"];
           Dictionary<string, List<SiteMap>> dic = new Dictionary<string, List<SiteMap>>();
           foreach (var path in paths)
           {
               dic.Add(path, new List<SiteMap>());
           }

           foreach (SPListItem item in list.Items)
           {
               string key = "/" + item["Language"] + "/" + item["DeviceType"] + "/";
               if (dic.ContainsKey(key) && (!string.IsNullOrWhiteSpace("" + item["Title"])))
               {
                   SiteMap siteMapItem = new SiteMap();
                   siteMapItem.ID = item.ID.ToString();
                   siteMapItem.PageTitle = "" + item["PageTitle"];
                   siteMapItem.ContentHtml = "" + item["ContentHtml"];
                   dic[key].Add(siteMapItem);
               }
           }

           foreach (var key in dic.Keys)
           {
               if (dic[key].Count > 0)
               {
                   System.Web.Script.Serialization.JavaScriptSerializer jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                   var josnResult = "window.SiteMap =" + jsonSerializer.Serialize(dic[key]);
                   var path = web.Url + "/SiteData" + key + "SiteMap.json.js";
                   WriteFileToSharePointSite(web, path, josnResult);
               }
           }
       }

       public static void GenerateJsonForContactInformationPage(SPWeb web, string[] paths)
       {
           SPList list = web.Lists["ContactInformationPage"];
           Dictionary<string, List<ContactInformationPage>> dic = new Dictionary<string, List<ContactInformationPage>>();
           foreach (var path in paths)
           {
               dic.Add(path, new List<ContactInformationPage>());
           }

           foreach (SPListItem item in list.Items)
           {
               string key = "/" + item["Language"] + "/" + item["DeviceType"] + "/";
               if (dic.ContainsKey(key) && (!string.IsNullOrWhiteSpace("" + item["Title"])))
               {
                   ContactInformationPage contactInfoPageItem = new ContactInformationPage();
                   contactInfoPageItem.ID = item.ID.ToString();
                   contactInfoPageItem.PageTitle = "" + item["PageTitle"];
                   contactInfoPageItem.ContentHtml = "" + item["ContentHtml"];
                   dic[key].Add(contactInfoPageItem);
               }
           }

           foreach (var key in dic.Keys)
           {
               if (dic[key].Count > 0)
               {
                   System.Web.Script.Serialization.JavaScriptSerializer jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                   var josnResult = "window.ContactInformationPage =" + jsonSerializer.Serialize(dic[key]);
                   var path = web.Url + "/SiteData" + key + "ContactInformationPage.json.js";
                   WriteFileToSharePointSite(web, path, josnResult);
               }
           }
       }

    }
}
